# CMake 最低版本要求
cmake_minimum_required(VERSION 3.16)

# 项目名称、版本和语言
project(CloudClient VERSION 0.1 LANGUAGES CXX)

# 启用 Qt 的自动代码生成功能 (MOC, UIC, RCC)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# --- 查找 Qt6 ---
# 优先使用环境变量 QT6_PATH 作为查找提示
# find_package 会在 CMAKE_PREFIX_PATH 中指定的路径下查找
if(DEFINED ENV{QT6_PATH})
  # 将环境变量中的路径添加到 CMake 搜索路径列表的前面，提高优先级
  list(PREPEND CMAKE_PREFIX_PATH "$ENV{QT6_PATH}")
  set(QT_BIN_DIR "$ENV{QT6_PATH}/bin")         # 记住 bin 目录，方便后面用
  message(STATUS "Using QT6_PATH hint: $ENV{QT6_PATH}")
endif()

# 查找 Qt6 的 Widgets 组件。REQUIRED 表示如果找不到就报错。
# 这会定义 Qt6::Widgets 等目标。
find_package(Qt6 REQUIRED COMPONENTS
        Widgets
        Network
        OpenGL
        OpenGLWidgets
        Multimedia
)
# find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) # 这是查找Qt6或Qt5的写法，但既然有QT6_PATH，就优先明确找Qt6

# 显示找到的 Qt 版本信息
message(STATUS "Found Qt version: ${Qt6_VERSION} installed in: ${Qt6_DIR}")

# ---- 库 ----
# FFmpeg
include_directories($ENV{FFMPEG_HEADER})
set(FFMPEG_LIB_DIR $ENV{FFMPEG_LIB})
link_directories(
        ${FFMPEG_LIB_DIR}
)

# --- 项目源文件 ---
set(PROJECT_SOURCES
        main.cpp
        CloudAssistant.h
        CloudAssistant.cpp
        WinSock2Guard.h
        # 如果有 .qrc 文件，也添加到这里，例如：
        # resources.qrc
)

# 把 UI 路径加入 include 搜索路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/UI)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/UI/brown)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/UI/center)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/UI/list)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/UI/title)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/UI/tool)
# 递归搜集 UI 下所有 .cpp 和 .h
file(GLOB_RECURSE UI_SOURCES
        CONFIGURE_DEPENDS                      # CMake 3.12+：如果目录文件有增删，重新 cmake 时会自动更新
        "${CMAKE_CURRENT_SOURCE_DIR}/UI/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/UI/*.h"
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Pusher/capture)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Pusher/rtmp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Pusher)
file(GLOB_RECURSE PUSH_SOURCES
        CONFIGURE_DEPENDS                      # CMake 3.12+：如果目录文件有增删，重新 cmake 时会自动更新
        "${CMAKE_CURRENT_SOURCE_DIR}/Pusher/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/Pusher/*.h"
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Codec)
file(GLOB_RECURSE CODEC_SOURCES
        CONFIGURE_DEPENDS                      # CMake 3.12+：如果目录文件有增删，重新 cmake 时会自动更新
        "${CMAKE_CURRENT_SOURCE_DIR}/Codec/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/Codec/*.h"
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Net)
file(GLOB_RECURSE NET_SOURCES
        CONFIGURE_DEPENDS                      # CMake 3.12+：如果目录文件有增删，重新 cmake 时会自动更新
        "${CMAKE_CURRENT_SOURCE_DIR}/Net/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/Net/*.h"
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Puller)
file(GLOB_RECURSE PULL_SOURCES
        CONFIGURE_DEPENDS                      # CMake 3.12+：如果目录文件有增删，重新 cmake 时会自动更新
        "${CMAKE_CURRENT_SOURCE_DIR}/Puller/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/Puller/*.h"
)

# --- 创建可执行文件 ---
# 使用 Qt 6 提供的函数创建可执行文件
# 它会自动处理 MOC, UIC, RCC (因为 CMAKE_AUTO* 设置为 ON)
qt_add_executable(CloudClient
    ${PROJECT_SOURCES}
    ${UI_SOURCES}
    ${PUSH_SOURCES}
    ${CODEC_SOURCES}
    ${NET_SOURCES}
    ${PULL_SOURCES}

    res.qrc
)

# ① 让 WinSock2Guard.h 作为整个工程的预编译头
target_precompile_headers(CloudClient PRIVATE
        "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/WinSock2Guard.h>"
)

# --- 链接库 ---
# 链接 Qt Widgets 库
# 使用 Qt6::Widgets (或版本无关的 Qt::Widgets) 目标
target_link_libraries(CloudClient PRIVATE Qt6::Widgets
    Qt6::Network
    Qt6::OpenGL
    Qt6::OpenGLWidgets
    Qt6::Multimedia
    gdi32         # GDI
    user32        # USER
    ole32         # OLE / COM
    ksuser        # Kernel Streaming


    avformat.lib
    avcodec.lib
    avdevice.lib
    avfilter.lib
    avutil.lib
    postproc.lib
    swresample.lib
    swscale.lib
)


# ---------- 让 VS 能直接运行 ----------
if(MSVC)
    # ① 调试器环境：把 Qt/bin 加入 PATH
    set_target_properties(CloudClient PROPERTIES
        VS_DEBUGGER_ENVIRONMENT
            "PATH=%PATH%;${QT_BIN_DIR}"
        VS_DEBUGGER_WORKING_DIRECTORY
            "$<TARGET_FILE_DIR:CloudClient>"
    )
endif()

# ---------- 自动部署 Qt 运行时 ----------
if(WIN32 AND EXISTS "${QT_BIN_DIR}/windeployqt.exe")

    # 先准备一个列表，按 Qt 版本决定是否排除 OpenGL SW / ANGLE
    set(WDEPLOY_OPTS
        --compiler-runtime              # 复制 MSVC 运行时
        --qmldir \"${CMAKE_SOURCE_DIR}\"  # 如果日后用到 QML
    )

    # Qt >= 6.5 改用 --no-opengl-sw，老版本还支持 --no-angle
    if(Qt6_VERSION VERSION_LESS "6.5.0")
        list(APPEND WDEPLOY_OPTS --no-angle)
    else()
        list(APPEND WDEPLOY_OPTS --no-opengl-sw)
    endif()

    add_custom_command(TARGET CloudClient POST_BUILD
        COMMAND "${QT_BIN_DIR}/windeployqt.exe"
                ${WDEPLOY_OPTS}
                \"$<TARGET_FILE:CloudClient>\"
        COMMENT "Running windeployqt to copy Qt runtime libraries..."
    )
endif()

# --- 设置特定平台的目标属性 ---
# 设置 macOS Bundle 和 Windows 可执行文件属性
set_target_properties(CloudClient PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER "com.example.CloudClient" # macOS 应用唯一标识符
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE           # 在 macOS 上创建 Bundle
    WIN32_EXECUTABLE TRUE      # 在 Windows 上创建窗口应用程序 (隐藏控制台)
)

# --- 安装规则 ---
# 包含 GNU 标准安装目录变量 (如 CMAKE_INSTALL_BINDIR)
include(GNUInstallDirs)

# 定义安装目标
install(TARGETS CloudClient
    BUNDLE DESTINATION .  # macOS Bundle 安装到哪里
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # 可执行文件安装目录
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # (如果目标是库) 库文件安装目录
    # ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # (如果目标是库) 静态库/导入库安装目录
)
